package nio.bytebuffer;

import java.nio.ByteBuffer;

public class Put {

    public static void main(String[] args) {

        byte[] byteArrayIn1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
        ByteBuffer bytebuffer = ByteBuffer.allocate(10);
        int getArrayIndex = 0;
        while (getArrayIndex < byteArrayIn1.length) {
            // 下面代码的作用就是判断：缓冲区的剩余和数组的剩余谁少
            int readLength = Math.min(bytebuffer.remaining(), byteArrayIn1.length - getArrayIndex);
            bytebuffer.put(byteArrayIn1, getArrayIndex, readLength);
            bytebuffer.flip();
            byte[] getArray = bytebuffer.array();
            for (int i = 0; i < bytebuffer.limit(); i++) {
                System.out.print(getArray[i] + " ");
            }
            getArrayIndex = getArrayIndex + readLength;
            System.out.println();
            bytebuffer.clear();
        }

    }

}
